package com.example.demo2.demo03;

import com.example.demo2.rabbitmq.RabbitMQConnection;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @Description: 生产者-交换机类型-fanout  发布订阅模式
 * 生产者发送一条消息，经过交换机转发到多个不同的队列，多个不同的队列就多个不同的消费者
 * @Author: mingtian
 * @CreateDate: 2021/1/16 09:41
 * @Version: 1.0
 */
public class ProducerFanout {

    /**
     * 定义交换机的名称
     */
    private static final String EXCHANGE_NAME = "fanout_exchange";

    public static void main(String[] args) throws IOException, TimeoutException {
        //  创建Connection
        Connection connection = RabbitMQConnection.getConnection();
        // 创建Channel
        Channel channel = connection.createChannel();
        // 通道关联交换机
        channel.exchangeDeclare(EXCHANGE_NAME, "fanout", true);
        String msg = "hello,this is msg";
        // 投递消息到交换机
        channel.basicPublish(EXCHANGE_NAME, "", null, msg.getBytes());
        channel.close();
        connection.close();
    }

}